
module Backend = Backend_ObjC

let _ =
  let input_file_name = Sys.argv.(1) in
  let output_file_name =
    if input_file_name = "-" then
      "a.m"
    else
      Filename.chop_extension (Filename.basename input_file_name) ^ ".m"
  in
  let ch_in =
    if input_file_name = "-" then
      stdin
    else
      open_in input_file_name
  and ch_out = open_out output_file_name in
  let emit = output_string ch_out in
  let lexbuf = Lexing.from_channel ch_in in
  let expr = Parser.main Lexer.token lexbuf in
  let (classes, expr') = Compiler.compile (Filename.chop_extension output_file_name) expr in
    List.iter Backend.generate_class classes;
    emit "#import <Value.h>\n";
    emit "#import <Prelude.h>\n";
    List.iter
      (fun (AstO.Class (class_name, _, _, _)) -> emit (Printf.sprintf "#import <%s.h>\n" class_name))
      classes;
    emit "int main() {\n";
    emit "  Value* res = ";
    Backend.compile_expr ch_out expr';
    emit ";\n";
    emit "  printf(\"%s\\n\", [res toString]);\n";
    emit "}\n";
    close_in ch_in;
    close_out ch_out

